package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.EcologyPoolRedPackReceive;
import com.sc.nft.entity.dto.EcologyPoolRedPackReceiveDO;
import com.sc.nft.mapper.EcologyPoolRedPackReceiveMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * 品牌红包领取记录(sc_dao_red_pack_receive)数据DAO
 *
 * @author jinpinhai
 */
@Slf4j
@Repository
public class EcologyPoolRedPackReceiveDao extends ServiceImpl<EcologyPoolRedPackReceiveMapper, EcologyPoolRedPackReceive> {

    public boolean updateSendStatus(Long receiveId) {
        EcologyPoolRedPackReceive ecologyPoolRedPackReceive = new EcologyPoolRedPackReceive();
        ecologyPoolRedPackReceive.setStatus(Boolean.TRUE);
        LambdaQueryWrapper<EcologyPoolRedPackReceive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolRedPackReceive::getId, receiveId);
        return baseMapper.update(ecologyPoolRedPackReceive, wrapper) > 0;
    }

    public List<EcologyPoolRedPackReceive> countUserReceive(Long userId, Long redPackId) {
        LambdaQueryWrapper<EcologyPoolRedPackReceive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(userId), EcologyPoolRedPackReceive::getUserId, userId);
        wrapper.eq(EcologyPoolRedPackReceive::getEcologyPoolRedPackId, redPackId);
        return baseMapper.selectList(wrapper);
    }


    public Page<EcologyPoolRedPackReceive> redPackParticipatePage(Long userId, Long daoId, Integer pageNo, Integer pageNum) {
        LambdaQueryWrapper<EcologyPoolRedPackReceive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyPoolRedPackReceive::getUserId, userId);
        wrapper.eq(EcologyPoolRedPackReceive::getDaoId, daoId);
        wrapper.orderByDesc(EcologyPoolRedPackReceive::getId);
        return page(new Page<>(pageNo, pageNum), wrapper);
    }

    public Page<EcologyPoolRedPackReceiveDO> receivePage(Integer pageNo, Integer pageNum, Long daoRedPackId, String userTel) {
        if (Objects.isNull(daoRedPackId)) {
            return new Page<>(pageNo, pageNum);
        }
        return baseMapper.receivePage(new Page<>(pageNo, pageNum), daoRedPackId, userTel);
    }
}